package com.linkage.jz.bankruptcy.fragment.Project;

import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.linkage.jz.bankruptcy.BankruptcyApp;
import com.linkage.jz.bankruptcy.Consts;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.activity.Person.PersonSettingReadMeActivity;
import com.linkage.jz.bankruptcy.activity.Project.CreateProjectActivity;
import com.linkage.jz.bankruptcy.activity.RegistLogin.AccountLoginActivity;
import com.linkage.jz.bankruptcy.event.RefreshEvent;
import com.linkage.jz.bankruptcy.fragment.Message.MsgBBSFragment;
import com.linkage.jz.bankruptcy.fragment.Message.MsgNoticeFragment;
import com.linkage.jz.bankruptcy.fragment.Message.MsgWordsFragment;
import com.linkage.jz.bankruptcy.fragment.Project.MyProFragment;
import com.linkage.jz.bankruptcy.fragment.Project.PublicProFragment;
import com.linkage.jz.bankruptcy.utils.SPUtils;
import com.linkage.jz.bankruptcy.widget.PublicNoticeDialog;
import com.linkage.jz.bankruptcy.widget.ShapeLoadingDialog;
import com.linkage.seapeak.activity.chat.SearchProjectActivity;
import com.linkage.seapeak.activity.register.RegisterActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by caobotao on 16/1/4.
 */
public class ProjectFragment extends Fragment implements View.OnClickListener{
    @Nullable
    private static final String TAG = PersonSettingReadMeActivity.class.getSimpleName();
    private Button topBtn01, topBtn02;
    private View leftBt;
    private ViewPager mViewPager;
    private TextView id_dcphGroup_unreadbt1,id_dcphGroup_unreadbt2;
    private TabPagerAdapter mPagerAdapter;
    ArrayList<Fragment> fragments = new ArrayList<Fragment>();
    private ShapeLoadingDialog shapeLoadingDialog ;
    private ImageView iv_right_bt;
    View view = null;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(view == null){
            view = inflater.inflate(R.layout.fragment_tab_project, container, false);
            shapeLoadingDialog = new ShapeLoadingDialog(getActivity());

            ActionBar bar = getActivity().getActionBar();
            if(bar != null){
                bar.hide();
            }
            init(view);
            EventBus.getDefault().register(this);
        }

        return view;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(RefreshEvent event) {
        if (TextUtils.equals(event.getType(), RefreshEvent.TYPE_MY_PRO)) {
            changeFragmentFocus(0);
            mViewPager.setCurrentItem(0);
        }
        if (TextUtils.equals(event.getType(), RefreshEvent.TYPE_PU_PRO)) {
            changeFragmentFocus(1);
            mViewPager.setCurrentItem(1);
        }
    }

    private void init(View view){
        MyProFragment groupChildrenCollect = new MyProFragment();
        PublicProFragment groupChildrenHasBuy = new PublicProFragment();
        fragments.add(groupChildrenCollect);
        fragments.add(groupChildrenHasBuy);

        topBtn01 = (Button) view.findViewById(R.id.id_dcphGroup_topBtn01);
        topBtn02 = (Button) view.findViewById(R.id.id_dcphGroup_topBtn02);
//        topBtn03 = (Button) view.findViewById(R.id.id_dcphGroup_topBtn03);
        id_dcphGroup_unreadbt1 = view.findViewById(R.id.id_dcphGroup_unreadbt1);
        id_dcphGroup_unreadbt2 = view.findViewById(R.id.id_dcphGroup_unreadbt2);
//        id_dcphGroup_unreadbt3 = view.findViewById(R.id.id_dcphGroup_unreadbt3);
        id_dcphGroup_unreadbt1.setVisibility(View.GONE);
        id_dcphGroup_unreadbt2.setVisibility(View.GONE);
//        id_dcphGroup_unreadbt3.setVisibility(View.GONE);
        leftBt = view.findViewById(R.id.left_bt);
        leftBt.setOnClickListener(this);
        topBtn01.setOnClickListener(this);
        topBtn02.setOnClickListener(this);
        iv_right_bt = (ImageView) view.findViewById(R.id.right_bt);
        iv_right_bt.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                String userToken = SPUtils.getInstance(Consts.SPNAME).getString("userToken", "");
                if ("".equalsIgnoreCase(userToken)) {
                    noticeDialog = new PublicNoticeDialog(getActivity(),onClickListener,
                            1,"提示","操作此功能，需要请您先注册/登录",
                            "取消","确认");
                    noticeDialog.show();
                    return;
                }

                CreateProjectActivity.start(getActivity());
            }
        });
//        topBtn03.setOnClickListener(this);
        mViewPager = (ViewPager) view.findViewById(R.id.id_dcphGroup_viewPager);
        mPagerAdapter = new TabPagerAdapter(getChildFragmentManager(), fragments);

        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.invalidate();
        mPagerAdapter.notifyDataSetChanged();

        topBtn02.setBackgroundResource(R.drawable.fragment_tab_msg_corner_btn);
        topBtn01.setTextColor(getResources().getColor(R.color.page_top));
        changeFragmentFocus(0);
        mViewPager.setCurrentItem(0);
    }

    private PublicNoticeDialog noticeDialog;
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (v.getId()) {
                case R.id.ll_close:
                    noticeDialog.dismiss();
                    break;

                case R.id.btn_chancel_notice:
//                   Boolean flag =  noticelxDialog.cb_agree.isChecked();
                    noticeDialog.dismiss();

                    break;

                case R.id.btn_save_notice:
                    RegisterActivity.start(getActivity());
                    noticeDialog.dismiss();
                    break;

            }
        }
    };
    /*
     * 自定义FragmentStatePagerAdapter适配器来显示滑动Fragment
     * */
    private class TabPagerAdapter extends FragmentStatePagerAdapter implements
            ViewPager.OnPageChangeListener {

        private ArrayList<Fragment> objects;

        public TabPagerAdapter(FragmentManager fm, ArrayList<Fragment> objects) {
            super(fm);
            this.objects = objects;
            mViewPager.setOnPageChangeListener(this);
        }

        @Override
        public Fragment getItem(int position) {
            return objects.get(position);
        }

        @Override
        public int getCount() {
            return objects.size();
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            //滑动的监听事件
            changeFragmentFocus(position);
        }
    }


    private void changeFragmentFocus(int focus){
        if(focus == 0){
            topBtn01.setTextColor(getResources().getColor(R.color.text_333_color));
            topBtn01.setTextSize(16);
            topBtn02.setTextSize(14);
            topBtn02.setTextColor(getResources().getColor(R.color.text_999_color));
        }else if (focus == 1){
            topBtn01.setTextColor(getResources().getColor(R.color.text_999_color));
            topBtn02.setTextColor(getResources().getColor(R.color.text_333_color));
            topBtn02.setTextSize(16);
            topBtn01.setTextSize(14);
        }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.id_dcphGroup_topBtn01:
                changeFragmentFocus(0);
                mViewPager.setCurrentItem(0);
                break;
            case R.id.id_dcphGroup_topBtn02:
                changeFragmentFocus(1);
                mViewPager.setCurrentItem(1);
                break;
            case R.id.left_bt:
                startActivity(new Intent(getActivity(), SearchProjectActivity.class));
                break;
            default:
                break;
        }
    }
}
